<?php

class Hotel extends MY_Controller {
	
	// array with vars to be used in the layout
	private $data_layout;
	
	/**
	 * 'link'=>'name' menu entries
	 * @var array
	 */
	private $menuItems = array (
		'Hotel/index'				=>	'principal',
		'Hotel/offers'				=>	'ofertas',
		'Hotel/extras'				=>	'extras',
		'Hotel/clients'				=>	'clientes',
		'Hotel/listReservations'	=>	'reservas feitas',
	);
	
	/**
	 * view's to be used with 'template' libray 
	 * @var array
	 */
	var $views=array(
		'hotelBodyMain'				=>'hotel/content/hotelBodyMain',
		'hotelBodyOffers'			=>'hotel/content/hotelBodyOffers',
		'hotelBodyExtras'			=>'hotel/content/hotelBodyExtras',
		'hotelBodyClients'			=>'hotel/content/hotelBodyClients',
		//'hotelBodySchool'			=>'hotel/content/hotelBodySchool',
		'hotelBodyReservation'		=>'hotel/content/hotelBodyReservation',		
		'hotelBodyListReservations'	=>'hotel/content/hotelBodyListReservations',	
	);
	
	
	/**
	 * Constructor 
	 */
	function Hotel() {
		parent::__construct();
		
		if ($this->ion_auth->is_group("hotel")) {
			
			$this->load->helpers(array('html'));
			
			$this->load->library(array('carabiner','template','fuelmenu','ion_auth'));
			
			$this->load->model('Hotel_model');
			
			$this->lang->load('hotel');
			
			$this->carabiner->css('reset.css');
			$this->carabiner->css('hotel.css');
		
			$this->_configTemplate();
		
			$this->_setData();
			
			$this->_tmp();
		}
		else {		// not a Hotel user
			redirect('Login/index');
		}
	}
	
	
	/**
	 * config the template data library
	 */
	private function _configTemplate() {
		$this->template->title('Radical Booking Hotel: '.$this->ion_auth->get_user()->company);
		$this->template->enable_parser(false);
		$this->template->set_layout('hotelLayout');
		$this->template->set_partial('clientForm', 		'hotel/partials/hotelClientForm');
	}
	
	
	/**
	 * Set global var to be forward to all views
	 */
	private function _setData() {
		
		// data about the current logged in hotel
		$user = $this->Hotel_model->getUserInfo($this->ion_auth->get_user()->id);
		
		$this->template->set('user',$user);
		
		$this->template->set('ajaxUrl',$this->config->item('hotel_ajaxUrl'));
	}
	
	
	/**
	 * Create a menu with all schools associated to the current hotel
	 * Note: This isnt needed for now, because only one school will be 
	 * associated with an hotel
	 */
	private function _schoolListMenu() {
		
		$schools = $this->Hotel_model->getHotelASschool($this->ion_auth->get_user()->id);
		
		$nav = array();
		
		foreach ($schools as $key => $value) {
			$nav['Hotel/school/'.$key] = $value;
		}
		
		$param = array(
			'active_class'			=>'current',
			'use_titles'			=>false,
			'container_tag_class'	=>'navigation',
		);
		$this->fuelmenu->initialize($param);
		
		$menu = $this->fuelmenu->render($nav,null,null,'basic');
		
		return $menu;
	}
	
	
	/**
	 * menu with navigation links 
	 */
	private function _navigationMenu() {

		// set this link with class="current"
		$active = $this->uri->rsegment(1).$this->uri->slash_rsegment(2,'leading');
		
		$param = array(
			'active_class'			=>'current',
			'use_titles'			=>false,
			'container_tag_class'	=>'navigation',
		);
		$this->fuelmenu->initialize($param);
		
		// render the menu
		return $this->fuelmenu->render($this->menuItems,$active,NULL,'basic');
	}
	
	
	/**
	 * use this function to load temporary test data 
	 */
	private function _tmp() {
		
	}
	
	
	
	
	
	
	
	
	
	
	
	public function index() {
		
		// witch libraries we need in this view? only those will be loaded in the head view file
		if ($this->remoteResources)
			$this->template->set('groupLibraries',array('jquery'));
		else 
			$this->template->set('groupLibraries',array('jquery_local'));
		
		$this->template->set('navigationMenu',$this->_navigationMenu());
		
		$id = $this->Hotel_model->getSchoolID($this->ion_auth->get_user()->id);
		
		$this->template->build($this->views['hotelBodyMain']);
	}
	
	/**
	 * show available school offers 
	 */
	public function offers() {
		
		// witch libraries we need in this view? only those will be loaded in the head view file
		if ($this->remoteResources)
			$this->template->set('groupLibraries',array('jquery','jqueryui'));
		else
			$this->template->set('groupLibraries',array('jquery_local','jqueryui_local'));
		
		$this->template->set('navigationMenu',$this->_navigationMenu());
		
		$schoolId = $this->Hotel_model->getSchoolID($this->ion_auth->get_user()->id);
		
		if ($schoolId) {
			$offers = $this->Hotel_model->getAvailableOffers($schoolId);
			$this->template->set('offers',$offers);
		}
		else {
			redirect('Hotel');
		}
		
		$this->template->build($this->views['hotelBodyOffers']);
	}
	
	
	public function extras() {
		
		// witch libraries we need in this view? only those will be loaded in the head view file
		if ($this->remoteResources)
			$this->template->set('groupLibraries',array('jquery'));
		else
			$this->template->set('groupLibraries',array('jquery_local'));
		
		$this->template->set('navigationMenu',$this->_navigationMenu());
		
		$this->template->build($this->views['hotelBodyExtras']);
	}
	
	
	public function clients() {
		
		// witch libraries we need in this view? only those will be loaded in the head view file
		if ($this->remoteResources)
			$this->template->set('groupLibraries',array('jquery','jqueryui','datatables'));
		else
			$this->template->set('groupLibraries',array('jquery_local','jqueryui_local','datatables_local'));
		
		$this->template->set('navigationMenu',$this->_navigationMenu());
		
		//$this->carabiner->css('hotelClient.css');
		$this->carabiner->js('clients.js');
		
		$this->template->build($this->views['hotelBodyClients']);
		
	}
	
	
	public function listReservations() {
		
		// witch libraries we need in this view? only those will be loaded in the head view file
		if ($this->remoteResources)
			$this->template->set('groupLibraries',array('jquery','jqueryui','datatables'));
		else
			$this->template->set('groupLibraries',array('jquery_local','jqueryui_local','datatables_local'));
		
		$this->template->set('navigationMenu',$this->_navigationMenu());
		
		$this->template->build($this->views['hotelBodyListReservations']);
		
	}
	
	
	/**
	 * Function called when the offer reservation link is pressed  
	 * @param int $offerId identification of the offer to be reserved
	 */
	function reservation($offerId = null) {
		
		if ($this->remoteResources)
			$this->template->set('groupLibraries',array('jquery','jqueryui','datatables'));
		else
			$this->template->set('groupLibraries',array('jquery_local','jqueryui_local','datatables_local'));
		
		$this->template->set('offerId',$offerId);
		
		$userId = $this->ion_auth->get_user()->id;
		$this->template->set('userId', $userId);
		
		$this->template->set('navigationMenu',$this->_navigationMenu());
		
		$this->carabiner->js('clients.js');
		
		$schoolId = $this->Hotel_model->getSchoolID($userId);
		
		if ($schoolId) {
			$offers = $this->Hotel_model->getAvailableOffers($schoolId);
			$this->template->set('offers',$offers);
		}
		else {
			redirect('Hotel');
		}
		
		$this->template->build($this->views['hotelBodyReservation']);
	}
	
	
	
	
	
	
	
	
	public function createClientAction() {
		
		// validate 'post' data here
		
		$okMsg 		= '{"Result":"OK"}';
		
		$this->firephp->log($_POST);
		
		// mysql DATE format
		$birthday = date('Y-m-d',strtotime($_POST['birthday']));
		
		$_POST['birthday'] = $birthday;
		
		$this->Hotel_model->createClient($_POST);
	}
	
	public function readClientAction() {
		//$this->firephp->log($_POST);
		$var1 = '
		{
  			"aaData": [
	    		{
	      			"name": "",
	      			"address": "Internet Explorer 4.0",
	      			"email": "Win 95+",
			      	"phone": "4",
	      			"birthday": "X"
	    		}
    		]
		}
		';
		$var2 = '
		{
  			"demo": [
	    		[
	      			"antonio",
	      			"Felgueiras",
	      			"antonio@mail.com",
			      	"91847383",
	      			"20-12-1990"
	    		],
	    		[
	      			"antonio",
	      			"Felgueiras",
	      			"antonio@mail.com",
			      	"91847383",
	      			"20-12-1990"
	    		],
	    		[
	      			"maria",
	      			"Felgueiras",
	      			"antonio@mail.com",
			      	"91847383",
	      			"20-12-1990"
	    		]
    		],
    		"test": [
    			[
    				"antonio",
	      			"Felgueiras",
	      			"antonio@mail.com",
			      	"91847383",
	      			"20-12-1990"
	      		]
    		],
    		"object": [
    			{
    				"nome": "sonia",
    				"morada": "porto",
    				"email": "sonia@mail.com",
    				"telefone": "58349",
    				"data":	"25-10-1987"
    			}
    		]
		}
		';
		
		$var3 = '
		{
			"demo": [
	    		[
	      			"antonio",
	      			"Felgueiras",
	      			"antonio@mail.com",
			      	"91847383",
	      			"20-12-1990"
	    		],
	    		[
	      			"antonio",
	      			"Felgueiras",
	      			"antonio@mail.com",
			      	"91847383",
	      			"20-12-1990"
	    		],
	    		[
	      			"maria",
	      			"Felgueiras",
	      			"antonio@mail.com",
			      	"91847383",
	      			"20-12-1990"
	    		]
	    	]
		}
		
		';
		
		$empty = '{"demo":[]}';
		
		//echo $var2;
		echo $this->Hotel_model->readClient();
		
	}
	
	public function updateClientAction() {
		
	}
	
	public function deleteClientAction() {
		
	}
	
	
	/**
	 * function called when reservation button is pressed and 
	 * one client is selected in the list
	 * @_POST fields: client_id, offer_id
	 * @todo check if the client is not the in reservation that is going to be created
	 */
	public function createReservationAction() {
		
		$ok 		= '{"Result":"OK","msg":"Reserva efectuada com sucesso"}';
		$errClient 	= '{"Result":"ERROR","msg":"Cliente ja esta registado"}';
		$errGeneric = '{"Result":"ERROR","msg":"Não foi possivel fazer a reserva"}';
		
 		$reserved = $this->Hotel_model->createReservation($_POST);
 		
 		if ($reserved)
 			echo $ok;
 		else
 			echo $errGeneric; 
	}
	
	public function readReservationAction() {
		$res = $this->Hotel_model->readReservation();	
		//echo 'hello';
		//$this->firephp->log($res);
		echo $res;
	}
	
	public function updateReservationAction() {
		
	}
	
	public function deleteReservationAction() {
		
	}
	
	public function readScheduleAction() {
		$this->firephp->log($_GET);
		$tmp = '{"Result":"OK"}';
		$schedules = $this->Hotel_model->readSchedule($_GET['offerId'],$_GET['userId']);
		//$this->firephp->log($schedules);
		echo json_encode($schedules);
	}
	
	
	/**
	 * 
	 * @todo validate GET fields
	 */
	public function readOfferAction() {
		
		$offer = $this->Hotel_model->readOffer($_GET['offerId'],$_GET['userId']);
		//$this->firephp->log($offer);
		echo json_encode($offer);
	}
	
	/**
	* Logout the Hotel user from the system
	*/
	public function logout() {
		$this->ion_auth->logout();
		redirect('Main/index');
	}
	

	
	
	
	
	
	
	
	
	/**
	* Function called when a school is choosen in the menu
	* Note: for now this function isn't need ...
	* @param int  of the school selected in the menu
	*/
	function school($schoolID = null) {
	
		// when hotel user presses a school link, this id is used to identify the school
		$this->template->set('id',$schoolID);
	
		// witch libraries we need in this view? only those will be loaded in the head view file
		if ($this->remoteResources)
		$this->template->set('groupLibraries',array('jquery'));
		else
		$this->template->set('groupLibraries',array('jquery_local'));
	
		// get all available offers from the given school id
		if ($id != null) {
			$offers = $this->Hotel_model->getAvailableOffers($schoolID);
			$this->template->set('offers',$offers);
		}
		else {
			// ... no id ... show main hotel page
			redirect('Hotel');
		}
	
		$this->template->build($this->views['hotelBodySchool']);
	}
	
	
	
	/**
	 * For test purposes only
	 */
	public function tmp() {
		
		
// 		$this->db->join('sh','sh.school_id = users.id');
// 		$query = $this->db->get();
// 		$row = $query->result();
// 		$this->firephp->log($row);
		
 		
	}
	
}